package com.homer.gps.track.adapters;

import java.sql.SQLException;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.homer.gps.track.R;
import com.homer.gps.track.helper.UsefullStaff;
import com.homer.gps.track.persist.DatabaseHelper;
import com.homer.gps.track.persist.PlaceModel;
import com.homer.gps.track.persist.Repository;
import com.homer.gps.track.persist.StopModel;
import com.j256.ormlite.android.apptools.OpenHelperManager;

public class StopsArrayAdapter extends ArrayAdapter<StopModel>{
	
	private final String LOG_TAG = "PlacesAdapter";

	private List<StopModel> stops = null;
	private Activity context = null;
	private boolean deleteMode = false;
	
	private Repository repository;
	private DatabaseHelper dbHelper = null;
	
	public StopsArrayAdapter(final Activity context, final int textViewResourceId, final List<StopModel> stops) {
		super(context, textViewResourceId, stops);
		this.context = context;
		this.stops = stops;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = convertView;
		
		this.repository = new Repository(getHelper());
		
		if (rowView == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
			rowView = inflater.inflate(R.layout.stops_list_item, parent, false);
		}
		
		TextView placeName = (TextView) rowView.findViewById(R.id.place_name);
		TextView placeAddress = (TextView) rowView.findViewById(R.id.place_address);
		TextView stopDate = (TextView) rowView.findViewById(R.id.start_date);
		ImageView typePicture = (ImageView) rowView.findViewById(R.id.place_type_icon);
		
		StopModel stop = (StopModel) stops.get(position);
		stopDate.setText(UsefullStaff.getFormatedDateAndTime(stop.getStartDateTime(), "dd/MM/yyyy"));
		
		PlaceModel place = null;
		try {
			place = repository.lookupPlacesForStop(stop);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if (place != null) {
			placeName.setText(String.valueOf(place.getName()));
			String addr = (String) place.getParsedAddress();
			if (addr.equals("")) {
				placeAddress.setText(R.string.no_value);
			}else{
				placeAddress.setText(addr);
			}
			typePicture.setImageResource(UsefullStaff.getPlaceTypeImageResource(place.getType()));
		}	
		
//		ImageView deleteBt = (ImageView )rowView.findViewById(R.id.delete_bt);
//		deleteBt.setTag(place);
//		if (deleteMode) {
//			deleteBt.setVisibility(View.VISIBLE);
//		} else {
//			deleteBt.setVisibility(View.INVISIBLE);
//		}
		
		
 
		return rowView;
	}
	
	public void setDeleteMode(boolean value) {
		deleteMode = value;
		notifyDataSetChanged();	
	}
	
	private DatabaseHelper getHelper() {
	    if (dbHelper == null) {
	        dbHelper = (DatabaseHelper)OpenHelperManager.getHelper(context, DatabaseHelper.class);
	    }
	    return dbHelper;
	}
}
